using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.SceneManagement;
using UnityEngine;

namespace MG
{
    [CustomEditor(typeof(MeshRenderer))]
    [CanEditMultipleObjects]
    public class MeshRendererEditor : DecoratorEditor
    {
        Renderer _m_renderer;

        public MeshRendererEditor() : base("MeshRendererEditor")
        {
        }
        
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            EditorGUILayout.Space();
            _m_renderer = target as Renderer;
            
            string[] layerNames = new string[SortingLayer.layers.Length];
            for (int i = 0; i < SortingLayer.layers.Length; i++)
                layerNames[i] = SortingLayer.layers[i].name;

            int baseValue = SortingLayer.GetLayerValueFromName(layerNames[0]);

            int layerValue = SortingLayer.GetLayerValueFromID(_m_renderer.sortingLayerID) - baseValue;
            layerValue = EditorGUILayout.Popup("Sorting Layer", layerValue, layerNames);

            SortingLayer layer = SortingLayer.layers[layerValue];
            string oldLayerName = _m_renderer.sortingLayerName;
            int oldLayerID = _m_renderer.sortingLayerID;
            int oldOrder = _m_renderer.sortingOrder;
            _m_renderer.sortingLayerName = layer.name;
            _m_renderer.sortingLayerID = layer.id;
            _m_renderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", _m_renderer.sortingOrder);

            if (oldLayerName != _m_renderer.sortingLayerName ||
                oldLayerID != _m_renderer.sortingLayerID ||
                oldOrder != _m_renderer.sortingOrder)
            {
                EditorUtility.SetDirty(_m_renderer);
                PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage();
                if (stage != null)
                    EditorSceneManager.MarkSceneDirty(stage.scene);
            }
        }
    }

    [CustomEditor(typeof(SkinnedMeshRenderer))]
    [CanEditMultipleObjects]
    public class SkinnedMeshRendererEditor : DecoratorEditor
    {
        Renderer _m_renderer;

        public SkinnedMeshRendererEditor() : base("SkinnedMeshRendererEditor")
        {
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            EditorGUILayout.Space();
            _m_renderer = target as Renderer;

            string[] layerNames = new string[SortingLayer.layers.Length];
            for (int i = 0; i < SortingLayer.layers.Length; i++)
                layerNames[i] = SortingLayer.layers[i].name;

            int baseValue = SortingLayer.GetLayerValueFromName(layerNames[0]);

            int layerValue = SortingLayer.GetLayerValueFromID(_m_renderer.sortingLayerID) - baseValue;
            layerValue = EditorGUILayout.Popup("Sorting Layer", layerValue, layerNames);

            SortingLayer layer = SortingLayer.layers[layerValue];
            string oldLayerName = _m_renderer.sortingLayerName;
            int oldLayerID = _m_renderer.sortingLayerID;
            int oldOrder = _m_renderer.sortingOrder;
            _m_renderer.sortingLayerName = layer.name;
            _m_renderer.sortingLayerID = layer.id;
            _m_renderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", _m_renderer.sortingOrder);

            if (oldLayerName != _m_renderer.sortingLayerName ||
                oldLayerID != _m_renderer.sortingLayerID ||
                oldOrder != _m_renderer.sortingOrder)
            {
                EditorUtility.SetDirty(_m_renderer);
                PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage();
                EditorSceneManager.MarkSceneDirty(stage.scene);
            }
        }
    }
}